﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using Pulog.Models;
using System.Text;

namespace Pulog.Controllers
{
    public class AboutFormViewModel
    {
        public int x;
        public int y;

    }

    [HandleError]
    public class HomeController : Controller
    {
        AboutFormViewModel about = new AboutFormViewModel();

        public ActionResult Index(int page)
        {
            ViewData["Title"] = "Home Page";

            PostRepository p = new PostRepository();
            List<Post> posts = p.GetPostList(page);
            var userList = new List<User>();

            foreach (Post po in posts)
            {
                userList.Add(new User().GetUserByID(po.authorID));
            }

            ViewData["User"] = userList;
            return View("Index", posts);
        }

        public ActionResult ShowPost(string slug)
        {
            Post p = new PostRepository().GetPostBySlug(slug);
            return View("ShowPost", p);
        }

        public ActionResult ShowPostComments(int postID)
        {
            List<Comment> c = new CommentRepository().GetCommentsByPostId(postID);
            return PartialView("ShowPostComments", c);
        }

        public ActionResult AddComment()
        {
            return View();
        }
        
        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult AddComment(FormCollection form)
        {
            Comment comment = new Comment();
            
            comment.AuthorName = form["AuthorName"];
            comment.CommentContent = form["CommentContent"];
            comment.AuthorIP = form["AuthorIP"];
            comment.PostID = Convert.ToInt32(form["PostID"]);
            comment.CommentTime = Convert.ToDateTime(form["CommentTime"]);
            return View(comment);
           
        }

        //public ActionResult EditComment(int id)
        //{
        //    Comment c = new Comment();
        //    c = c.GetCommentById(id);

        //    return View(c);
        //}

        [AcceptVerbs(HttpVerbs.Post)]
        public ActionResult EditComment(FormCollection form)
        {
            return View();
        }

        public ActionResult Categories()
        {

            return View();
        }

        public ActionResult About()
        {
            Random random = new Random();
            about.x = random.Next(0,9);
            about.y = random.Next(0,9);
            Session["result"] = about.x + about.y;
            return View(about);
        }

        public string GetContact(string answer)
        {
            if (answer == Session["result"].ToString())
            {
                return "Name: Shawn Jiang <br /> TEL = +64-9-4798096 <br /> CellPhone = +64-211836589<br />  QQ = 17238526<br />" +
                    "Email=xuancn@gmail.com<br /> MSN = xuancn@hotmail.com<br /> Gtalk = xuancn@gmail.com<br /> Skype = xuancn";
            }
            else
            {
                return "Sorry, wrong answer... <a id=\"tryAgain\" href=\"javascript:window.location.reload();\">Try again?</a>";
            }
        }
    }
}

